package com.company.linkedlist;

import com.company.linkedlist.model.Boy;

/**
 * @author shenhan
 * @create 2020/1/28 14:20
 * @description
 */
public class CircleSingleLinkedList {
    // 先创建一个first节点 当前无编号
    private Boy first = new Boy(-1);

    // 添加
    public void addBoy(int nums) {
        if (nums < 1) {
            System.out.println("数据不正确");
            return;
        }
        Boy curBoy = null;
        for (int i = 0; i < nums; i++) {
            Boy boy = new Boy(i + 1);
            if (i == 0) {
                first = boy;
                first.setNext(first);
                curBoy = first;
            } else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }

    // 遍历环形链表
    public void showBoy() {
        if (first == null) {
            System.out.println("链表为空");
            return;
        }
        Boy curBoy = first;
        while (true) {
            System.out.println("小孩的编号："+curBoy.getNo());
            if (curBoy.getNext() == first) {
                break;
            }
            curBoy = curBoy.getNext();
        }

    }
}
